package Gr

import (
	"database/sql"
	"fmt"
	"db2s/ETL"
	"db2s/global"
	"db2s/ref"
	"strings"
)

func (fm *FuncMe) Set(parameter string, value any) (err error) {
	return ref.UpdateStructJsonValue(fm, parameter, value)
}

func (fm FuncMe) Result(object string) global.Return {
	switch object {
	default:
		return global.Return{}
	}
}
func (fm FuncMe) Name(Con *sql.DB) (result global.Return, err error) {
	var event = "[Name]"
	var execSql []string
	fm.Parameter.Con = Con
	execSql = append(execSql, fmt.Sprintf("select ROUTINE_NAME from INFORMATION_SCHEMA.ROUTINES where  ROUTINE_TYPE = 'FUNCTION' AND ROUTINE_SCHEMA ='%v' %v",
		fm.Parameter.Object.Schema, func() string {
			switch {
			case fm.Parameter.Object.Table == "*":
				return ""
			case strings.Contains(fm.Parameter.Object.Table, "%"):
				return fmt.Sprintf(" and ROUTINE_NAME like '%%%v%%'", fm.Parameter.Object.Table)
			default:
				return fmt.Sprintf(" and ROUTINE_NAME = '%v'", fm.Parameter.Object.Table)
			}
		}()))
	if result, err = forExecQuerySql(event, fm.Parameter, ETL.StringGarth, execSql); err != nil || result.Result == nil {
		return
	}
	return
}
func (fm FuncMe) Existence(Con *sql.DB) (result global.Return, err error) {
	var event = "[Existence]"
	var execSql []string
	fm.Parameter.Con = Con
	execSql = append(execSql, fmt.Sprintf("select ROUTINE_NAME from INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'FUNCTION' and ROUTINE_SCHEMA = '%v' and ROUTINE_NAME = '%v'", fm.Parameter.Object.Schema, fm.Parameter.Object.Table))
	if result, err = forExecQuerySql(event, fm.Parameter, ETL.String, execSql); err != nil || result.Result == nil {
		return
	}
	switch {
	case strings.EqualFold(fmt.Sprintf("%v", result.Result), fm.Parameter.Object.Table):
		result.Result = true
	default:
		result.Result = false
	}
	return
}
func (fm FuncMe) FuncDefinition(Con *sql.DB) (result global.Return, err error) {
	return
}
